<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <!-- 利用canvas将图片转为base64格式 -->
    <img src="" id="img1">
</body>
<script>
    var img1 = document.getElementById('img1');
    var canvas = document.createElement("canvas");
    var ctx = canvas.getContext('2d'),
        img = new Image;
    img.src = "./p1.png";
    img.onload = function () {
        canvas.height = img.height;
        canvas.width = img.width;
        ctx.drawImage(img, 0, 0);
        dataURL = canvas.toDataURL("image/jpeg");
        img1.src = dataURL;
    };
</script>

</html>